package com.ssi.myshop.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ssi.myshop.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity {
    private static final String TAG = "BaseActivity";

    private Unbinder mBind;
    private FrameLayout mContent;
    private View mLoadingView;

    public enum State {
        NONE, SUCCESS, LOADING, ERROR, Empty
    }

    private State currentState = State.NONE;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());
        mContent = findViewById(R.id.base_activity_container);
        mBind = ButterKnife.bind(this);
        initView();
        initPresent();
        initEvent();
        loadStateView();
    }

    private void loadStateView() {
        mLoadingView = LayoutInflater.from(this).inflate(R.layout.fragment_loading, mContent, false);
        mContent.addView(mLoadingView);
        setUpState(State.NONE);
    }

    protected abstract void initPresent();

    protected abstract void initEvent();

    protected abstract void initView();


    protected abstract int getLayoutResId();

    protected void setUpState(State state) {
        this.currentState = state;
        mLoadingView.setVisibility(currentState == State.LOADING ? View.VISIBLE : View.GONE);
        mContent.setVisibility(currentState == State.NONE ?  View.GONE : View.VISIBLE );
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBind != null) {
            this.mBind.unbind();
        }
    }
}
